<?php
/**
 * Viewer ini menerima data dalam sebuah array 'data', yang minimal berisi:
 * ['metadata']
 *		['n_fields']	= banyaknya field pada tabel
 *		['fields']		= array berisi informasi field-field tabel
 *			['name']		= nama field pada tabel
 *			['alias']		= displayed name dari field
 *			['type']		= tipe field tabel (converted)
 *			['values']	= list of possible values, only for ENUM
 *			['nullable']	= if true: cannot enter null as value
 *			['editable']	= if false: read only
 *			['viewable']	= if false: hidden
 *		['fields_list']	= array berisi daftar field-field tabel	 
 *		['fields_aliases']
 *						= array berisi mapping alias field-field tabel
 *		['keys']		= array berisi keys dari tabel 
 *			['n_tables']	 = no of table involved
 *			['pri_table']	 = name of primary table
 *			['primary']	=> Primary Keys
 *				[table]					of table 'table'
 *				['name']  = key name, usually PRIMARY
 *				['value'] = single value or array if composite 
 *
 * ['data']
 *			['n_all_records']	= banyaknya record pada table
 *			['first']			= array berisi record pertama table
 *			['previous']		= array berisi record sebelumnya pada table
 *			['current']			= array berisi record yang dipilih
 *			['next']			= array berisi record selanjutnya pada table
 *			['last']			= array berisi record terakhir table
 */

//---------- EKSTRAK $data menjadi $metadata dan $data
extract($data);

//---------- GET SETTINGS FROM CONFIG FILE
include "_get_settings.php";

//---------- BUILD HEADER TABLE
// $table_fields: daftar fields yang viewable
// $table_fileds_idx: index dari $table_fileds pada $metadata['fields'];
$table_fields = array();
$table_fields_idx = array();
foreach ($metadata['fields'] as $idx => $val) {
	$table_field_idx = $idx;
	$table_field = $val['name'];

	if ($val['viewable']) {
		$table_fields[] = $table_field;
		$table_fields_idx[] = $table_field_idx;
	}
}

//print_r_pre($table_fields);

// $table_header['fields'] = $table_fields, tetapi nama field diubah menjadi aliasnya, jika didefinisikan
$table_header = array();
foreach($table_fields as $idx => $value) {
	$table_header['fields'][$idx] = $metadata['fields_aliases'][$value];
}

//print_r_pre($table_header);

//---------- BUILD ISI TABLE
$table_content = array();
foreach ($table_fields as $idx => $value) {
	$table_content[] = array($table_header['fields'][$idx], $data['current'][$value]);
}

//print_r_pre($table_content);

//---------- BUILD FOOTER TABLE
$table_footer = array();

//---------- TAMBAHKAN ACTIONS
// modify content
foreach ($table_content as $ridx => $rval) {
	$type = $metadata['fields'][$table_fields_idx[$ridx]]['type'];
	
	// in_place actions		
	if (isset($actions['in_place'][$ridx]) && (!is_empty($actions['in_place'][$ridx])) && (!is_null_or_false($actions['in_place'][$ridx])))
		$table_content[$ridx][1] = str_from_template($actions['in_place'][$ridx], array('VALUE' => $table_content[$ridx][1]));
	
	// jika tipe == FILE atau tipe == IMAGE, tambahkan aksi untuk download
	if ((strcasecmp($type, 'FILE')==0) || (strcasecmp($type, 'IMAGE')==0)) {
		$filename = $metadata['fields'][$table_fields_idx[$ridx]]['properties']['filename'];		
		$filename = str_from_template($filename, $data['current']);
		$this->common->create_download_button($filename, $table_content[$ridx][1]);
	}
	// jika tipe == PASSWORD, mask the data
	if (strcasecmp($type, 'PASSWORD')==0) {
		$this->common->mask_password($table_content[$ridx][1]);
	}
}

// modify footer

$table_footer_str  = '<td align="center" colspan="0">';
$record = $this->common->create_uri_record_part($data['current']);
$first_record = $this->common->create_uri_record_part($data['first']);
if (isset($data['previous']['id'])) {
	$prev_record = $this->common->create_uri_record_part($data['previous']);
} else {
	$prev_record = $this->common->create_uri_record_part($data['last']);
}

if (isset($data['next']['id'])) {
	$next_record = $this->common->create_uri_record_part($data['next']);
} else {
	$next_record = $this->common->create_uri_record_part($data['first']);
}
$last_record = $this->common->create_uri_record_part($data['last']);
$arr_template = array_merge(array('RECORD_ID' => $data['current']['id'], 'RECORD' => $record, 'FIRST_RECORD' => $first_record, 'PREV_RECORD' => $prev_record, 'NEXT_RECORD' => $next_record, 'LAST_RECORD' => $last_record), $label_template);

if (is_array($actions['nav']['left'])) {
	foreach ($actions['nav']['left'] as $aidx => $aval) {	
		$table_footer_str .= str_from_template($aval, $arr_template)._WIDE_SEP;
	}
}

if (is_array($actions['nav']['center'])) {
	foreach ($actions['nav']['center'] as $aidx => $aval) {	
		$table_footer_str .= str_from_template($aval, $arr_template);
	}
}

if (is_array($actions['nav']['right'])) {
	foreach ($actions['nav']['right'] as $aidx => $aval) {	
		$table_footer_str .= _WIDE_SEP.str_from_template($aval, $arr_template);
	}
}
$table_footer_str  .= '</td>';

$table_footer[] = $table_footer_str;


//---------- DISPLAY TABLE
$table_data = compact('table_header', 'table_content', 'table_footer', 'label', 'table_records_per_page');
if (!is_null_or_false($data['current'])) {
	$this->load->view('viewer/columnar_table', $table_data);
} else {
?>
	<div class="box important" style="margin: .8em">No records found.</div>
<?php
}

/* EOF views/viewer/columnar.php */
